package com.wc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wc.api.UserService;
import com.wc.model.User;

@Controller
public class loginController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("tologin")
	public String tologin(){
		return "login";
	}
	
	@RequestMapping(value="index1",produces="text/html")
	public String home(){
		return "index";
	}
	
	@RequestMapping("logInto")
	@ResponseBody
	public String login(HttpServletRequest request){
		String email=(String) request.getParameter("usremail");
		String password=(String) request.getParameter("password");
		User user=userService.findUserByEmail(email);
		if(user==null){
			return "00001";
		}else{
			if(password.equals(user.getPassword())){
				System.out.println("2222");
				return "00000";
			}else{
				return "00002";
			}
		}
	}
	
	@RequestMapping("signup")
	@ResponseBody
	public String signup(HttpServletRequest request){
		String useremail=(String) request.getParameter("usremail");
		String username=(String) request.getParameter("usrname");
		String password=(String) request.getParameter("password");
		System.out.println(useremail);
		User user=new User();
		user.setEmail(useremail);
		user.setPassword(password);
		user.setUsername(username);
		if (userService.addUser(user)) {
			return "00000";
		}else{
			return "00001";
		}
	}
	
	@RequestMapping("main")
	@ResponseBody
	public String main(){
		return "welcome back~";
	}
}
	